#include "GameScene.h"
#include <time.h>

USING_NS_CC;

CCScene* GameScene::scene()
{
	// 'scene' is an autorelease object
	CCScene *scene = CCScene::create();

	// 'layer' is an autorelease object
	GameScene *layer = GameScene::create();

	// add layer as a child to scene
	scene->addChild(layer);

	// return the scene
	return scene;
}

// on "init" you need to initialize your instance
bool GameScene::init()
{
	//////////////////////////////
	// 1. super init first
	if ( !CCLayer::init() )
	{
		return false;
	}

	CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();
	CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();

	/////////////////////////////
	// 2. add a menu item with "X" image, which is clicked to quit the program
	//    you may modify it.

	// add a "close" icon to exit the progress. it's an autorelease object
	CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
		"CloseNormal.png",
		"CloseSelected.png",
		this,
		menu_selector(GameScene::menuCloseCallback));

	pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,
		origin.y + pCloseItem->getContentSize().height/2));

	// create menu, it's an autorelease object
	CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
	pMenu->setPosition(CCPointZero);
	this->addChild(pMenu, 1);
	bullet = new Bullet();
	background	=	new Background("img_bg_level_3.png");
	addChild( background );
	CCSize size = CCDirector::sharedDirector()->getWinSize();
	player = CCSprite::create("ship.png");
	player->setScale(0.2f);
	player->setPosition(ccp(origin.x + visibleSize.width / 2 ,
		origin.y +  40));
	addChild(player);
	bullet = new Bullet(player);
	addChild(bullet);
	timeFactor		=	0.f;
	this->setTouchEnabled( true );
	this->scheduleUpdate();

	return true;
}

void GameScene::menuCloseCallback(CCObject* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT) || (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
	CCMessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
#else
	CCDirector::sharedDirector()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
	exit(0);
#endif
#endif
}

void GameScene::ccTouchesMoved(CCSet *pTouches, CCEvent *pEvent)
{
	/*if( this->airCraft )
	{
		this->airCraft->inputProcess( pTouches, pEvent );
	}*/
}

void GameScene::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
}
//
void GameScene::update( float deltaTime )
{
	//timeFactor	+=	deltaTime;
	//if( timeFactor >= 1.5f )
	//{
	//	char *s	=	rand() % 3 >= 2 ? "1003" : "1002";
	//	AirEnemy *airEnemy	=	new AirEnemy( s );
	//	if( airEnemy )
	//	{
	//		float randX = rand() % 450 + 50;
	//		airEnemy->setPosition( ccp( randX, 800.f ));
	//		objs->addObject( airEnemy );
	//		addChild( airEnemy );
	//	}
	//	timeFactor	=	0.f;
	//}
	bullet->update(deltaTime,player);
	//unsigned count	=	objs->count();
	//for ( unsigned i = 0; count != 0 && i < (count - 1); ++i )
	//{
	//	for ( unsigned j = i+1; j < count; ++j )
	//	{
	//		GameObject *gojA	=	dynamic_cast<GameObject*>(objs->objectAtIndex( i ));
	//		GameObject *gojB	=	dynamic_cast<GameObject*>(objs->objectAtIndex( j ));
	//		if( gojA->getBound().intersectsRect( gojB->getBound() ) )
	//		{
	//			if(gojA->isCollideWith( gojB ))
	//				gojA->saveCollideWith( gojB);

	//			if( gojB->isCollideWith( gojA ))
	//				gojB->saveCollideWith( gojA);
	//		}
	//	}
	//}

	//for ( unsigned i = 0; i < count; ++i )
	//{
	//	GameObject *goj	=	dynamic_cast<GameObject*>(objs->objectAtIndex( i ));
	//	goj->applyCollision();
	//}

	//for ( unsigned i = 0; i < count; ++i )
	//{
	//	GameObject *goj	=	dynamic_cast<GameObject*>(objs->objectAtIndex( i ));
	//	if( !goj->isAlive() )
	//	{
	//		objs->removeObjectAtIndex(i);
	//		removeChild( goj );
	//		goj->release();
	//		--i;
	//		// re-calculate obj counter
	//		count	=	objs->count();
	//	}
	//}
}
//
//void GameScene::addBullet( Bullet *bullet )
//{
//	addChild( bullet );
//	objs->addObject(bullet );
//}